﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GoShopping.BLL;

public partial class UserControls_CommonProductsList : System.Web.UI.UserControl
{
    private int pageSize = 15;
    private object lvDatasouce;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    private Range Analyse(string p)
    {
        Range rg = new Range();
        if (p.Contains("以下"))
        {
            p = p.Replace("以下", "");
            rg.IntMax = Convert.ToInt32(p);
            rg.IntMin = 0;
        }
        if (p.Contains("以上"))
        {
            p = p.Replace("以上", "");
            rg.IntMax = 100000;
            rg.IntMin = Convert.ToInt32(p);
        }
        if (p.Contains("-"))
        {
            string[] arr = p.Split('-');
            rg.IntMax = Convert.ToInt32(arr[1]);
            rg.IntMin = Convert.ToInt32(arr[0]);
        }
        return rg;

    }
    private void BindListView()
    {
        lvPrdts.DataSource = new GoShopping.BLL.ProductBean().GetProductList(Cache.Get("filter").ToString());
        lvPrdts.DataBind();
    }
    private string filter;
    public string FilterStr
    {
        get { return filter; }
        set { filter = value; }
    }
    private void SetPagesize()
    {
        if (this.lvPrdts.FindControl("DataPager1") != null)
        {
            ((DataPager)(this.lvPrdts.FindControl("DataPager1"))).PageSize = pageSize;
        }
    }
    public ListView LVPrdts
    {
        get { return lvPrdts; }
        set { lvPrdts = value; }
    }
    public object LVDataSource
    {
        get { return lvDatasouce; }
        set { lvDatasouce = value; }
    }
    public int PageSize
    {
        get { return pageSize; }
        set { pageSize = value; }
    }
    protected void lvPrdts_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        ((DataPager)(this.lvPrdts.FindControl("DataPager1"))).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        BindListView();
    }
    protected void lvPrdts_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Prdt")
        {
            //AddPrdtToRecentList(e.CommandArgument);
            Response.Redirect("CommonProducts.aspx?prdtId=" + e.CommandArgument);
        }
    }

    protected void lvPrdts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Label lblSaledPrice = e.Item.FindControl("lblSaledPrice") as Label;
            Label lblMarketPrice = e.Item.FindControl("lblMarketPrice") as Label;
            Label lblID = e.Item.FindControl("lblID") as Label;
            Image img = e.Item.FindControl("Image1") as Image;
            string imgSrc = "~" + GetWebPath(GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(lblID.Text), new ProductBean().GetProduct(Convert.ToInt32(lblID.Text)).ProductTypeID)).Substring(1, GetWebPath(GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(lblID.Text), new ProductBean().GetProduct(Convert.ToInt32(lblID.Text)).ProductTypeID)).Length - 1);
            img.ImageUrl = imgSrc;
            if (lblSaledPrice.Text == "")
            {
                lblSaledPrice.Text = lblMarketPrice.Text;
            }
        }
    }
    string GetWebPath(string path)
    {
        return GoShopping.Utility.WebPath.ConvertSpecifiedPathToRelativePath(this.Page, path).Replace("~", ".");
    }
}
